#ifndef _UTILEX_THREADPOOL_HPP_
#define _UTILEX_THREADPOOL_HPP_

#include <crt_util.h>

#ifdef __cplusplus

class CThreadPool : private CThread
{
public:
	CThreadPool() {

		s_memset(&pool, 0, sizeof(_threadpool_t));

	}
	virtual ~CThreadPool() {

		s_memset(&pool, 0, sizeof(_threadpool_t));
	}
	static int workfunccb(void* data, void* ctx)
	{
		_threadpool_t* parent_context = static_cast<_threadpool_t*>(data);
		rc_assert(parent_context != NULL, S_ERROR)
		CThreadPool* pool = static_cast<CThreadPool*>(parent_context->ctx);
		rc_assert(pool != NULL, S_ERROR)
		pool->ThreadPool_Run(data, ctx);

		return S_SUCCESS;
	}

	static int startfunccb(void* data, void* ctx)
	{
		return S_SUCCESS;
	}

	static int stopfunccb(void* data, void* ctx)
	{
		return S_SUCCESS;
	}

	static int errorfunccb(void* data, void* ctx)
	{
		return S_SUCCESS;
	}

	int StartThreadPool(int count) {
		
		pool.count = count;
		pool.ctx = this;
		pool.func.start = CThreadPool::startfunccb;
		pool.func.work = CThreadPool::workfunccb;
		pool.func.stop = CThreadPool::stopfunccb;
		pool.func.error = CThreadPool::errorfunccb;
		init_threadpool(&pool);

		return CThread::Start_Thread();
	}

	int StopThreadPool() {

		return CThread::Stop_Thread();
	}

	std_method(Thread_Init)() {

		return S_OK;
	}
	std_method(Thread_Run)() {

		this->ThreadPool_Start();
		start_threadpool(&pool);
		return S_OK;
	}
	std_method(Thread_UnInit)() {

		stop_threadpool(&pool);
		this->ThreadPool_Stop();
		return S_OK;
	}

protected:
	std_method(ThreadPool_Run)(void* data, void* ctx) PURE;
	std_method(ThreadPool_Start)() PURE;
	std_method(ThreadPool_Stop)() PURE;
	std_method(ThreadPool_Error)() PURE;
private:
	int	count;
	_threadpool_t	pool;
};


#endif


#endif
